﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LibUtilities.dal;
using System.Data;
using System.Data.SqlClient;

namespace CLibArtreve.dal
{
    public class RatingDAL : SqlDatabase
    {
        #region CONSTRUCTORS
        public RatingDAL(string connectionString) : base(connectionString) { }
        #endregion

        #region OVERRIDE METHODS
        public override string ToString()
        {
            return "DATA ACCES LAYER: " + this.GetType().ToString();
        }
        #endregion

        #region GET METHODS
        public DataSet GetAllRatings()
        {
            try
            {
                return ExecuteDataSetWithCommonQuery(Queries.SELECTALLRATINGS, null);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public DataSet GetRatingByArtId(Int64? id)
        {
            try
            {
                // MAKE A LIST OF PARAMETERS
                List<SqlParameter> parameters = new List<SqlParameter>();

                // MAKE PARAMETER
                SqlParameter parameter = new SqlParameter();
                parameter.DbType = DbType.Int64;
                parameter.Direction = ParameterDirection.Input;
                parameter.ParameterName = "rating_art_id";
                parameter.Value = id;
                parameters.Add(parameter);

                // EXECUTE
                return ExecuteDataSetWithCommonQuery(Queries.SELECTRATINGBYID, parameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public DataSet GetRatingByArtIdAndUserId(Int64? ratingUserId, Int64 ratingArtId)
        {
            try
            {
                // Check user ID
                if (ratingUserId == null)
                    throw new Exception("GetRatingByArtAndUserId: no UserId given!");

                // MAKE A LIST OF PARAMETERS
                List<SqlParameter> parameters = new List<SqlParameter>();

                // MAKE PARAMETER
                SqlParameter parameter = new SqlParameter();
                parameter.DbType = DbType.Int64;
                parameter.Direction = ParameterDirection.Input;
                parameter.ParameterName = "rating_art_id";
                parameter.Value = ratingArtId;
                parameters.Add(parameter);

                parameter = new SqlParameter();
                parameter.DbType = DbType.Int64;
                parameter.Direction = ParameterDirection.Input;
                parameter.ParameterName = "users_user_id";
                parameter.Value = ratingUserId;
                parameters.Add(parameter);

                // EXECUTE
                return ExecuteDataSetWithCommonQuery(Queries.SELECTRATINGBYARTIDANDUSERID, parameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public DataSet GetRatingByUserId(Int64? id)
        {
            try
            {
                // MAKE A LIST OF PARAMETERS
                List<SqlParameter> parameters = new List<SqlParameter>();

                // MAKE PARAMETER
                SqlParameter parameter = new SqlParameter();
                parameter.DbType = DbType.Int64;
                parameter.Direction = ParameterDirection.Input;
                parameter.ParameterName = "users_user_id";
                parameter.Value = id;
                parameters.Add(parameter);

                // EXECUTE
                return ExecuteDataSetWithCommonQuery(Queries.SELECTRATINGBYUSERID, parameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion

        #region INSERT METHODS
        public int InsertRating(Int64 ratingArtId, int ratingStars, Int64? ratingUserId)
        {
            try
            {
                // Check User ID
                if (ratingUserId == null)
                    throw new Exception("InsertRating: No user ID given!");

                // MAKE A LIST OF PARAMETERS
                List<SqlParameter> parameters = new List<SqlParameter>();

                // MAKE PARAMETERS
                SqlParameter parameter = new SqlParameter();
                parameter.DbType = DbType.Int64;
                parameter.Direction = ParameterDirection.Input;
                parameter.ParameterName = "rating_art_id";
                parameter.Value = ratingArtId;
                parameters.Add(parameter);

                parameter = new SqlParameter();
                parameter.DbType = DbType.Int16;
                parameter.Direction = ParameterDirection.Input;
                parameter.ParameterName = "rating_stars";
                parameter.Value = ratingStars;
                parameters.Add(parameter);

                parameter = new SqlParameter();
                parameter.DbType = DbType.Int64;
                parameter.Direction = ParameterDirection.Input;
                parameter.ParameterName = "users_user_id";
                parameter.Value = ratingUserId;
                parameters.Add(parameter);

                // EXECUTE
                int p_category = ExecuteNonQueryWithCommonQuery(Queries.INSERTRATING, parameters, true);
                if (p_category > 0)
                {
                    p_category = Convert.ToInt32(ExecuteScalarWithCommonQuery<decimal>(Queries.SELECTIDENTITYAFTERINSERTSQLSERVER));
                }
                return p_category;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        #endregion

        #region UPDATE METHODS
        public int UpdateArtByArtIdAndUserId(Int64? artId, int stars, Int64? usersUserId)
        {
            try
            {
                //MAKE SOME PARAMETERS
                List<SqlParameter> parameters = new List<SqlParameter>();

                SqlParameter parameter = new SqlParameter();
                if (artId != null)
                {
                    parameter = new SqlParameter();
                    parameter.DbType = DbType.Int64;
                    parameter.Direction = ParameterDirection.Input;
                    parameter.ParameterName = "rating_art_id";
                    parameter.Value = artId;
                    parameters.Add(parameter);
                }
                else
                {
                    parameter = new SqlParameter();
                    parameter.DbType = DbType.Int64;
                    parameter.Direction = ParameterDirection.Input;
                    parameter.ParameterName = "rating_art_id";
                    parameter.Value = DBNull.Value;
                    parameters.Add(parameter);
                }

                parameter = new SqlParameter();
                parameter.DbType = DbType.String;
                parameter.Direction = ParameterDirection.Input;
                parameter.ParameterName = "rating_stars";
                parameter.Value = stars;
                parameters.Add(parameter);

                parameter = new SqlParameter();
                parameter.DbType = DbType.String;
                parameter.Direction = ParameterDirection.Input;
                parameter.ParameterName = "users_user_id";
                parameter.Value = usersUserId;
                parameters.Add(parameter);

                return ExecuteNonQueryWithCommonQuery(Queries.UPDATERATING, parameters, true);

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
        }
        #endregion
    }
}
